#  存放公用的内容


from functools import wraps

from flask import current_app
from flask import g
from flask import session




def news_class_index(index):  # 使用过滤器过滤热梦门颜色
    if index == 1:
        return 'first'
    elif index == 2:
        return 'second'
    elif index == 3:
        return 'third'
    else:
        return ''


# 使用装饰器对用户登陆的信息进行封装
def user_login_data(view_func):

    @wraps(view_func)
    def wrapper(*args, **kwargs):
        #  通过session取出用户编号
        user_id = session.get('user_id')
        #  根据用户编号取出用户信息

        from info.models import User
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        #  使用g对象
        g.user=user

        return view_func(*args,**kwargs)

    return wrapper
